﻿using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System.Linq.Expressions;

namespace Microsoft.EntityFrameworkCore
{
    public class CustomBoolToStringConverter : BoolToTwoValuesConverter<string>
    {
        public CustomBoolToStringConverter(string falseValue, string trueValue,
            bool isIgnoreCase = true,
            [CanBeNull] ConverterMappingHints mappingHints = null)
            : base(
                falseValue, trueValue,
                ToBool(trueValue, isIgnoreCase),
                 new ConverterMappingHints(size: Math.Max(falseValue.Length, trueValue.Length)).With(mappingHints))
        {

        }

        private static Expression<Func<string, bool>> ToBool(string trueValue, bool isIgnoreCase)
        {
            if (!isIgnoreCase)
            {
                return value => !string.IsNullOrWhiteSpace(value) && value == trueValue;
            }

            trueValue = trueValue.ToLower();

            return value => !string.IsNullOrWhiteSpace(value) && value.ToLower() == trueValue;
        }

    }
}
